package com.tiancheng.trade.merchant.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.web.model.request.OrderMerchantSettleReqVO;
import com.tiancheng.trade.commom.web.model.response.SettlementRuleResVO;
import com.tiancheng.trade.merchant.enums.MerchantSettleRuleStatusEnum;
import com.tiancheng.trade.merchant.model.MerchantSettleRule;
import com.tiancheng.trade.merchant.service.IMerchantSettleRuleService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kellen
 * @date 2021/7/22
 */
@RestController
@RequestMapping("/api/merchantSettleRule")
public class MerchantSettleRuleController {
    @Autowired
    private IMerchantSettleRuleService merchantSettleRuleService;

    /**
     * 根据商户编码获取清分规则
     *
     * @param merchantCodes
     * @return
     */
    @GetMapping("/listRuleByMerchantCodes")
    public Result<List<MerchantSettleRule>> listRuleByMerchantCodes(@RequestParam("list") List<String> merchantCodes) {
        QueryWrapper<MerchantSettleRule> ruleQueryWrapper = new QueryWrapper<>();
        ruleQueryWrapper.eq("rule_status", MerchantSettleRuleStatusEnum.EFFECTIVE.getValue());
        ruleQueryWrapper.in("merchant_code", merchantCodes);

        return Result.success(merchantSettleRuleService.list(ruleQueryWrapper));
    }
    /**
     * 根据商户清分规则编码查询
     * @param merchantCods
     * @return
     */
    @PostMapping("/findByMerchantCodes")
    public Result<List<SettlementRuleResVO>> findByMerchantCodes(@RequestBody List<String> merchantCods) {
        if(CollectionUtils.isEmpty(merchantCods)){
            return Result.fail("参数为空",null);
        }
        return Result.success(merchantSettleRuleService.findByMerchantCodes(merchantCods));
    }

    /**
     * 根据商户清分规则编码查询
     * @param merchantRuleCodes
     * @return
     */
    @PostMapping("/findByRuleCodes")
    public Result<List<SettlementRuleResVO>> findByMerchantRuleCodes(@RequestBody List<String> merchantRuleCodes) {
        if(CollectionUtils.isEmpty(merchantRuleCodes)){
            return Result.fail("参数为空",null);
        }
        return Result.success(merchantSettleRuleService.findByMerchantRuleCodes(merchantRuleCodes));
    }

    /**
     * 下单商户的清分规则
     */
    @PostMapping("/orderMerchantSettleRule")
    public Result<List<SettlementRuleResVO>> queryOrderMerchantSettleRule(@RequestBody OrderMerchantSettleReqVO orderMerchantSettleReqVO) {
        if(CollectionUtils.isEmpty(orderMerchantSettleReqVO.getMerchantCods())){
            return Result.fail("参数为空",null);
        }
        return Result.success(merchantSettleRuleService.queryOrderMerchantSettleRule(orderMerchantSettleReqVO));
    }
}
